<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>页面电子闹钟</title>
</head>
<body>
    <h2></h2>
    <script>       
        setInterval(showTime,1000);
        function showTime() {           
            var h2=document.querySelector("h2");
            var date=new Date();
            //获得年、月、日            
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var dates = date.getDate();
            //定义星期数组
            var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六', ];
            var day = arr[date.getDay()];
            // var day=date.getDay();
            //在h2标签中显示系统当前时间
            h2.innerHTML = (year + '年' + month + '月' + dates + '日 ' + day)+' '+date.toLocaleTimeString();
            clearInterval(timer);
        } 
        showTime();     
    </script>
</body>
</html>